#include "cvprocessor.h"
#include "source/logger.h"
#include "ImageProcessor.h"
#include "configmanager.h"
#include "cvprocessor.h"
#include <QThread>
#include <QVariantMap>
#include <opencv2/features2d.hpp>


CvProcessor::CvProcessor(QObject *parent):QObject(parent)
{

}


void CvProcessor::processImg(ImageRecord &imgcard)
{
    LogDebug << "Starting CvProcessor Img";
    cv::Mat img_src = imgcard.getStepImage(StepProcess::Reprogress);
    if (img_src.empty()) {
        LogError << "CvProcessor get image is empty";
        imgcard.setStepImage(StepProcess::Cv, img_src);
        imgcard.setState(ImageRecord::Fail);
        return;
    }
    // try {
    //     for (auto& algorithm : algorithms) {
    //         if(algorithm->getIsInit())
    //         {
    //             bool ret = algorithm->process(imgcard);
    //             if(ret==false )
    //             {
    //                 imgcard.setState(ImageRecord::Fail);
    //             }
    //             else if(ret==false &&
    //                      ParamValue.getParameter("base","ContinueAfterError")==false)
    //             {
    //                 imgcard.setStepImage(StepProcess::Cv,cv::Mat());
    //                 imgcard.setState(ImageRecord::Fail);
    //                 return;
    //             }
    //         }
    //     }
    // } catch (...) {
    //     LogError << "processImg Fault occurred....";
    //     imgcard.setStepImage(StepProcess::Cv,cv::Mat());
    //     imgcard.setState(ImageRecord::Fail);
    //     return;
    // }
    imgcard.setStepImage(StepProcess::Cv,cv::Mat());
    LogDebug << "CvProcessor:finish detection " ;
    return;
}
// void CvProcessor::clearAlgorithms(ImageProcessor *imgProcessor) {
//     for (auto &algorithm : algorithms) {
//         // 断开每个算法与信号的连接
//         disconnect(&ConfigManager::getInstance() , &ConfigManager::configDataChanged, algorithm.get(), &CVAlgorithm::configDataChanged);
//         disconnect(imgProcessor, &ImageProcessor::monitorImgChange, algorithm.get(), &CVAlgorithm::monitorImgChangeSlots);
//     }
//     algorithms.clear();
// }

// void CvProcessor::loadAlgorithms(ImageProcessor *imgProcessor)
// {
//     clearAlgorithms(imgProcessor);
//     QVariantMap algorithmsMap = ParamValue.getParameter(section,"cvProjectSelection").toMap();
//     for (auto it = algorithmsMap.begin(); it != algorithmsMap.end(); ++it) {
//         std::string name = it.key().toStdString();
//         bool isEnabled = it.value().toBool();
//         if (isEnabled)
//         {
//             auto algorithm = CvAlgorithmFactory::createAlgorithm(name);
//             connect(&ConfigManager::getInstance() , &ConfigManager::configDataChanged, algorithm.get(), &CVAlgorithm::configDataChanged);
//             connect(imgProcessor, &ImageProcessor::monitorImgChange, algorithm.get(), &CVAlgorithm::monitorImgChangeSlots);
//             if (algorithm) {
//                 algorithms.push_back(std::move(algorithm));
//             }
//         }
//     }
// }

void CvProcessor::configDataChanged(const QString &category, const QString &key)
{

}
